package com.chris.easymallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chris.easymallproduct.dto.BaseAttrs;
import com.chris.easymallproduct.entity.AttrEntity;
import com.chris.easymallproduct.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallproduct.dao.ProductAttrValueDao;
import com.chris.easymallproduct.entity.ProductAttrValueEntity;
import com.chris.easymallproduct.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(Long spuId, List<BaseAttrs> baseAttrs) {
        if (baseAttrs != null && baseAttrs.size() > 0) {
            this.saveBatch(baseAttrs.stream()
                    .map(item -> {
                        ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                        productAttrValueEntity.setAttrId(item.getAttrId());
                        productAttrValueEntity.setAttrValue(item.getAttrValues());
                        productAttrValueEntity.setQuickShow(item.getShowDesc());
                        productAttrValueEntity.setSpuId(spuId);
                        AttrEntity attrEntity = attrService.getById(item.getAttrId());
                        if (attrEntity != null) {
                            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                        }
                        return productAttrValueEntity;
                    }).collect(Collectors.toList()));
        }


    }

    @Override
    public List<ProductAttrValueEntity> baseListForSpu(Long spuId) {
        return this.list(new LambdaQueryWrapper<ProductAttrValueEntity>()
                .eq(ProductAttrValueEntity::getSpuId, spuId));
    }

    @Override
    @Transactional
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> reqList) {
        ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
        productAttrValueEntity.setSpuId(spuId);
        this.removeById(productAttrValueEntity);

        List<ProductAttrValueEntity> collect = reqList.stream().peek(productAttrValueEntity1 -> {
            productAttrValueEntity1.setSpuId(spuId);
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}